<template>
  <div class="edit-form edit-dialog">
    <el-form
      ref="ruleFormRef"
      :model="form"
      :loading="loading"
      v-if="form.formArray && form.formArray.length > 0"
    >
      <el-descriptions
      style="margin-bottom:10px"
        :column="2"
        border
        :colon="true"
        label-class-name="custom-description-label-160"
        v-for="(item, index) in form.formArray"
        :key="index"
      >
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            名称
          </template>
          <el-form-item
            :prop="'formArray.' + index + '.name'"
            :rules="$formRules.input"
          >
            <el-input
              v-model="item.name"
              autocomplete="off"
              placeholder="请输入名称"
            />
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            编码
          </template>
          <el-form-item
            :prop="'formArray.' + index + '.code'"
            :rules="$formRules.input"
          >
            <el-input
              v-model="item.code"
              autocomplete="off"
              placeholder="请输入编码"
            />
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            状态
          </template>
          <el-form-item
            :prop="'formArray.' + index + '.status'"
            :rules="$formRules.select"
          >
            <pubDictSelect
            style="width:100%"
              v-model="item.status"
              dict-code="ZDZT"
              placeholder="请选择"
            />
          </el-form-item>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <span class="colorRed">*</span>
            类型
          </template>
          <el-form-item
            :prop="'formArray.' + index + '.type'"
            :rules="$formRules.select"
          >
            <pubDictSelect
            style="width:100%"
              v-model="item.type"
              dict-code="ZDLX"
              placeholder="请选择"
            />
          </el-form-item>
        </el-descriptions-item>
      </el-descriptions>
    </el-form>
    <div v-else>
      <el-empty description="暂无数据" />
    </div>
    <div class="btn-group">
      <el-button class="btn-form-close" @click="cancel">取消</el-button>
      <el-button
        class="btn-form-save"
        type="primary"
        @click="submitForm(ruleFormRef)"
      >
        保存
      </el-button>
    </div>
  </div>
</template>

<script>
export default {
  name: 'edit'
}
</script>

<script setup>
import { ElMessage } from 'element-plus'
import { getPidChildrenDict, getDictInfo, editDicts } from '@/api/system.js'
import { ref, reactive, onMounted } from 'vue'
const emit = defineEmits(['cancel'])
const props = defineProps({
  idEditChild: {
    type: Boolean,
    default: false
  },
  currentPid: {
    type: String,
    default: ''
  },
  currentCode: {
    type: String,
    default: ''
  }
})
const form = reactive({
  formArray: []
})
const loading = ref(false)
const initDict = async () => {
  loading.value = true
  try {
    if (props.idEditChild) {
      // 获取子级
      const res = await getPidChildrenDict({ id: props.currentCode })
      if (res.data && res.data.length > 0) {
        form.formArray = res.data
        loading.value = false
      }
    } else {
      // 获取父级
      const res = await getDictInfo(props.currentPid)
      if (res.data) {
        form.formArray.push(res.data)
        loading.value = false
      }
    }
  } catch (error) {
    loading.value = false
  }
}
onMounted(() => {
  initDict()
})
const ruleFormRef = ref()

const cancel = () => {
  emit('cancel', false)
}

const submitForm = formEl => {
  if (!formEl) return
  formEl.validate(async valid => {
    if (valid) {
      try {
        // 修改字典
        const res = await editDicts(form.formArray)
        if (res.succeed) {
          ElMessage({
            type: 'success',
            message: '修改成功'
          })
        }
        emit('cancel', false)
      } catch (error) {
        console.log('error', error)
      }
    }
  })
}
</script>

<style scoped>
.el-row {
  height: 28px;
  align-items: center;
  margin-bottom: 15px;
}
.el-col {
  height: 28px;
}

.form-item-btn-group :deep(.el-form-item__content) {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
